import Router from "koa-router";
import {
  loginController,
  registerController,
  codeController,
} from "../controllers/userController.js";
import User from "../models/user.js";

import { successResponse, errorResponse } from "../utils/responseHelper.js";

const router = new Router({ prefix: "/api/user" }); // 设置路由前缀

// 根据用户名获取用户信息
router.get("/:username", async (ctx) => {
  console.log(ctx.params);
  const { username } = ctx.params;
  try {
    const user = await User.findOne({
      where: { username: username },
    });

    if (!user) {
      ctx.status = 404;
      ctx.body = { message: "用户未找到" };
      return;
    }

    ctx.body = user;
  } catch (error) {
    console.error(error);
    ctx.status = 500;
    ctx.body = { message: "服务器错误" };
  }
});

// 根据用户名更新用户信息
router.post("/update/:username", async (ctx) => {
  const { username } = ctx.params;
  const { email, bio, phone, profile_picture } = ctx.request.body;

  try {
    const updated = await User.update(
      { email, bio, phone, profile_picture },
      {
        where: { username: username },
      }
    );
    console.log(updated);

    const updatedUser = await User.findOne({ where: { username: username } });
    successResponse(ctx, updatedUser);
  } catch (error) {
    console.error(error);
    ctx.status = 500;
    ctx.body = { message: "服务器错误" };
  }
});

/**
 * @swagger
 * /api/user/register:
 *   post:
 *     tags: [用户模块]
 *     summary: 注册
 *     description: 注册
 *     consumes:
 *       - application/json
 *     parameters:
 *       - name: user
 *         in: body
 *         required: true
 *         description: 注册
 *         schema:
 *           type: object
 *           required:
 *             - username
 *             - password
 *           properties:
 *             username:
 *               type: string
 *               description: 用户账号
 *             password:
 *               type: string
 *               description: 用户密码
 *             phone:
 *               type: string
 *               description: 用户邮箱
 *             code:
 *               type: string
 *               description: 验证码
 *     responses:
 *       200:
 *         description: 成功操作
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器错误
 */
router.post("/register", registerController);
/**
 * @swagger
 * /api/user/login:
 *   post:
 *     tags: [用户模块]
 *     summary: 登录
 *     description: 登录
 *     consumes:
 *       - application/json
 *     parameters:
 *       - name: user
 *         in: body
 *         required: true
 *         description: 登录
 *         schema:
 *           type: object
 *           required:
 *             - username
 *             - password
 *           properties:
 *             username:
 *               type: string
 *               description: 用户账号
 *             password:
 *               type: string
 *               description: 用户密码
 *
 *     responses:
 *       200:
 *         description: 成功操作
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器错误
 */
router.post("/login", loginController);

/**
 * @swagger
 * /api/user/code:
 *   post:
 *     tags: [用户模块]
 *     summary: 发送短信
 *     description: 发送短信
 *     consumes:
 *       - application/json
 *     parameters:
 *       - name: user
 *         in: body
 *         required: true
 *         description: 发送短信
 *         schema:
 *           type: object
 *           required:
 *             - phone
 *           properties:
 *             phone:
 *               type: string
 *               description: 手机号
 *     responses:
 *       200:
 *         description: 成功操作
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器错误
 */
router.post("/code", codeController);

export default router;
